window.addEventListener('load', () => {
  let token = getCookie('token');
  if (!token) {
    location.href = 'login.html'
    return;
  }
  // 渲染文章分类行
  ajax({
    type: 'get',
    url: baseUrl + '/system/category',
    success: function (res) {
      let data = res.data;
      let str = '<option value=""></option>';
      data.forEach(elem => {
        str += ` <option value="${elem.id}">${elem.name}</option>`
      });
      let type = document.querySelector('.categoryId');
      type.innerHTML = str
      layui.form.render();
    }
  });
  // 绑定提交事件 发布文章
  document.querySelector('.submit').addEventListener('click', function (e) {
    e.preventDefault();
    let title = document.querySelector('.title').value;
    let coverImg = document.querySelector('.coverImg').value;
    let categoryId = document.querySelector('.categoryId').value
    let content = editor.getHtml();
    let description = document.querySelector('.description').value
    console.log(content)
    ajax({
      type: 'post',
      url: baseUrl + '/article/publish',
      data: {
        categoryId,
        title,
        coverImg,
        content,
        description
      },
      headers: {
        Authorization: token
      },
      success: function (res) {
        if(res.code!=0){
          layer.msg(res.msg)
        }
        layer.msg('发送成功',{incon:1})
      }
    })
  })
  // 返回主页
document.querySelector('.back').addEventListener('click',function(e){
  e.preventDefault()
  location.href='index.html'
})
})